package com.qimei.onerice.dao.impl;

import com.qimei.onerice.dao.DishCategoryDao;
import com.qimei.onerice.entity.DishCategory;
import com.qimei.onerice.until.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DishCategoryDaoImpl implements DishCategoryDao {

	private static final String INSERT_DISH_CATEGORY = "INSERT INTO dish_categories (category_id, category_name) VALUES (?, ?)";
	private static final String SELECT_DISH_CATEGORY_BY_ID = "SELECT * FROM dish_categories WHERE category_id = ?";
	private static final String SELECT_ALL_DISH_CATEGORIES = "SELECT * FROM dish_categories";
	private static final String UPDATE_DISH_CATEGORY = "UPDATE dish_categories SET category_name = ? WHERE category_id = ?";
	private static final String DELETE_DISH_CATEGORY = "DELETE FROM dish_categories WHERE category_id = ?";

	@Override
	public int addDishCategory(DishCategory dishCategory) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_DISH_CATEGORY)) {
			preparedStatement.setInt(1, dishCategory.getCategoryId());
			preparedStatement.setString(2, dishCategory.getCategoryName());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public DishCategory getDishCategoryById(int categoryId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_DISH_CATEGORY_BY_ID)) {
			preparedStatement.setInt(1, categoryId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createDishCategoryFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<DishCategory> getDishCategoryList() {
		List<DishCategory> dishCategories = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_DISH_CATEGORIES);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				dishCategories.add(createDishCategoryFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return dishCategories;
	}

	@Override
	public int updateDishCategory(DishCategory dishCategory) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_DISH_CATEGORY)) {
			preparedStatement.setString(1, dishCategory.getCategoryName());
			preparedStatement.setInt(2, dishCategory.getCategoryId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deleteDishCategory(int categoryId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_DISH_CATEGORY)) {
			preparedStatement.setInt(1, categoryId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	// Helper 方法，从 ResultSet 中创建 DishCategory 对象
	private DishCategory createDishCategoryFromResultSet(ResultSet resultSet) throws SQLException {
		DishCategory dishCategory = new DishCategory();
		dishCategory.setCategoryId(resultSet.getInt("category_id"));
		dishCategory.setCategoryName(resultSet.getString("category_name"));
		return dishCategory;
	}
}